﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Net;
using System.Configuration;

namespace Project.dalSql
{
    public class Mail
    {
        /// <summary>
        /// 发送电子邮件
        /// </summary>
        /// <param name="MessageFrom">发件人邮箱地址 </param>
        /// <param name="MessageTo">收件人邮箱地址 </param>
        /// <param name="MessageSubject">邮件主题 </param>
        /// <param name="MessageBody">邮件内容 </param>
        /// <returns> </returns>
        //指定发送邮件的服务器地址或IP
        private static string Host = ConfigurationManager.AppSettings["Host"].ToString();  

        //指定发送邮件端口
        private static int Port = Convert.ToInt16(ConfigurationManager.AppSettings["Port"]);  

        //指定登录邮件服务器的用户名和密码,这里指的是发件人的用户名和密码
        private static string EmailLoginUser = ConfigurationManager.AppSettings["EmailLoginUser"].ToString();
        private static string EmailLoginPwd = ConfigurationManager.AppSettings["EmailLoginPwd"].ToString(); 


        public static bool Send(MailAddress MessageFrom, string MessageTo, string MessageSubject, string MessageBody)
        {
            MailMessage message = new MailMessage();

            message.From = MessageFrom;
            message.To.Add(MessageTo);              //收件人邮箱地址可以是多个以实现群发
            message.Subject = MessageSubject;
            message.Body = MessageBody;
            message.IsBodyHtml = true;              //是否为html格式
            message.Priority = MailPriority.High;   //发送邮件的优先等级

            SmtpClient sc = new SmtpClient();
            sc.UseDefaultCredentials = true;
           // sc.Host = "smtp.163.com";              //指定发送邮件的服务器地址或IP
            //sc.Port = 25;                           //指定发送邮件端口

            sc.Host = Host;              //指定发送邮件的服务器地址或IP
            sc.Port = Port;                        //指定发送邮件端口

            //sc.Credentials = new System.Net.NetworkCredential("XXX@163.com", "XXX"); //指定登录服务器的用户名和密码
            sc.Credentials = new System.Net.NetworkCredential(EmailLoginUser, EmailLoginPwd); //指定登录邮件服务器的用户名和密码,这里指的是发件人的用户名和密码
            try
            {
                sc.Send(message);       //发送邮件
            }
            catch (SmtpException ex)
            {
                return false;
            }
            return true;
        }
        /// <summary>
        /// 群发电子邮件
        /// </summary>
        /// <param name="MessageFrom">发件人邮箱地址 </param>
        /// <param name="MessageTo">收件人邮箱地址 </param>
        /// <param name="MessageSubject">邮件主题 </param>
        /// <param name="MessageBody">邮件内容 </param>
        /// <returns> </returns>
        public static bool Send(MailAddress MessageFrom, string[] MessageTo, string MessageSubject, string MessageBody)
        {
            MailMessage message = new MailMessage();

            message.From = MessageFrom;
            //message.To.Add(MessageTo);              //收件人邮箱地址可以是多个以实现群发
            foreach (var i in MessageTo)             //迭代收件人邮箱地址可以是多个以实现群发
            {
                message.To.Add(i);
            }
            message.Subject = MessageSubject;
            message.Body = MessageBody;
            message.Attachments.Add(new Attachment("E:\\技术部项目任务追踪表韩雪峰.doc")); //添加一个附件,这里也可以不添加
            message.IsBodyHtml = true;              //是否为html格式
            message.Priority = MailPriority.High;   //发送邮件的优先等级

            SmtpClient sc = new SmtpClient();
            sc.Host = "smtp.163.com";              //指定发送邮件的服务器地址或IP
            sc.Port = 25;                           //指定发送邮件端口
            //sc.Credentials = new System.Net.NetworkCredential("XXX@163.com", "XXX"); //指定登录服务器的用户名和密码
            sc.Credentials = new System.Net.NetworkCredential("handy0794@163.com", "handy_xuefeng"); //指定登录服务器的用户名和密码,这里指的是发件人的用户名和密码
            try
            {
                sc.Send(message);       //发送邮件
            }
            catch (SmtpException ex)
            {
                return false;
            }
            return true;
        }
    }

}
